﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using CCWin;

namespace SCCM
{
    public partial class HistorySearchForm : Skin_Mac
    {
        string bdz = null, kgs = null, kgg = null;
        DateTime dt1, dt2;
        private ArrayList dtx = new ArrayList();
        public ArrayList Dtx
        {
            get { return dtx; }
            set { dtx = value; }
        }
        private ArrayList yNO2;

        public ArrayList YNO2
        {
            get { return yNO2; }
            set { yNO2 = value; }
        }
        private ArrayList yNO;

        public ArrayList YNO
        {
            get { return yNO; }
            set { yNO = value; }
        }
        private ArrayList yCO;

        public ArrayList YCO
        {
            get { return yCO; }
            set { yCO = value; }
        }
        private ArrayList yO3;

        public ArrayList YO3
        {
            get { return yO3; }
            set { yO3 = value; }
        }
        private ArrayList yTem;

        public ArrayList YTem
        {
            get { return yTem; }
            set { yTem = value; }
        }
        private ArrayList yRH;

        public ArrayList YRH
        {
            get { return yRH; }
            set { yRH = value; }
        }
        private ArrayList yAto;

        public ArrayList YAto
        {
            get { return yAto; }
            set { yAto = value; }
        }
        public HistorySearchForm()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //StringBuilder sb = new StringBuilder();
            //foreach (Control c in Controls)
            //{
            //    if (c is CheckBox)
            //    {
            //        if (((CheckBox)c).Checked == true)
            //        {

            //            sb.Append(c.Text.ToString());
            //            sb.Append(",");
            //        }
            //    }
            //}
            //string wherein = sb.ToString().Remove(sb.Length - 1, 1);

            if (onlineCheckBox.Checked == false && bianxieCheckBox.Checked == false)
            {
                MessageBox.Show("请选择要查询的数据源：在线监测数据或便携式采集数据");
                return;
            }

            
            if (bianDianZhan.Text.Trim() == "" || bianDianZhan.Text == null)
            {
                MessageBox.Show("未选择变电站名称");
                return;
            }
            else if (kaiGuanShi.Text.Trim() == "" || kaiGuanShi.Text == null)
            {
                MessageBox.Show("未选择开关室名称");
                return;
            }
            else if (kaiGuanGui.Text.Trim() == "" || kaiGuanGui.Text == null)
            {
                MessageBox.Show("未选择开关柜编号");
                return;
            }
            else
            {


                int flag = 0;//在线监测
                if (onlineCheckBox.Checked == true && bianxieCheckBox.Checked == true)
                    flag = -1;
                else if (bianxieCheckBox.Checked == true)
                    flag = 1;

                chart1.Series[0].Points.Clear();
                chart1.Series[1].Points.Clear();
                chart1.Series[2].Points.Clear();
                chart1.Series[3].Points.Clear();
                //chart1.Series[4].Points.Clear();
                //chart1.Series[5].Points.Clear();
                //chart1.Series[6].Points.Clear();


                bdz = bianDianZhan.Text.Trim();
                kgs = kaiGuanShi.Text.Trim();
                kgg = kaiGuanGui.Text.Trim();
                dt1 = dateTimePicker1.Value;
                dt2 = dateTimePicker2.Value;
                int index = 0;
                //  MessageBox.Show(bdz + " " + kgs + " " + kgg + " " + dt.ToString());
                DataBase dataBase = new DataBase();
                ArrayList[] tmp = null;
                if (NO2CheckBox.Checked==true)
                {
                    tmp = dataBase.getHistory3(dt1, dt2, "NO2", bdz, kgs, kgg, flag);
                    yNO2 = tmp[0];
                    dtx = tmp[1];
                    chart1.Series[0].Points.DataBindXY(dtx, yNO2);
                    index++;
                }
                if (NOCheckBox.Checked == true)
                {
                    tmp = dataBase.getHistory3(dt1, dt2, "NO", bdz, kgs, kgg, flag);
                    yNO = tmp[0];
                    dtx = tmp[1];
                    chart1.Series[2].Points.DataBindXY(dtx, yNO);
                    index++;
                }
                if (COCheckBox.Checked == true)
                {
                    tmp = dataBase.getHistory3(dt1, dt2, "CO", bdz, kgs, kgg, flag);
                    yCO = tmp[0];
                    dtx = tmp[1];
                    chart1.Series[1].Points.DataBindXY(dtx, yCO);
                    index++;
                }
                if (O3CheckBox.Checked == true)
                {
                    tmp = dataBase.getHistory3(dt1, dt2, "O3", bdz, kgs, kgg, flag);
                    yO3 = tmp[0];
                    dtx = tmp[1];
                    chart1.Series[3].Points.DataBindXY(dtx, yO3);
                    index++;
                }
                //if (TemCheckBox.Checked == true)
                //{ 
                //    yTem = Standard(dataBase.getHistory3(dt1, dt2, "Tem", bdz, kgs, kgg)[0]);
                //    dtx = dataBase.getHistory3(dt1, dt2, "Tem", bdz, kgs, kgg)[1];
                //    chart1.Series[4].Points.DataBindXY(dtx, yTem);
                //    index++;
                //}
                //if (RHCheckBox.Checked == true)
                //{ 
                //    yRH = Standard(dataBase.getHistory3(dt1, dt2, "RH", bdz, kgs, kgg)[0]);
                //    dtx = dataBase.getHistory3(dt1, dt2, "RH", bdz, kgs, kgg)[1];
                //    chart1.Series[5].Points.DataBindXY(dtx, yRH);
                //    index++;
                //}
                //if (AtoCheckBox.Checked == true)
                //{
                //    yAto = Standard(dataBase.getHistory3(dt1, dt2, "Ato", bdz, kgs, kgg)[0]);
                //    dtx = dataBase.getHistory3(dt1, dt2, "Ato", bdz, kgs, kgg)[1];
                //    chart1.Series[6].Points.DataBindXY(dtx, yAto);
                //    index++;
                //}
    

                chart1.ChartAreas[0].CursorX.AutoScroll = true;
                chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
                chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
                chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
                chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
                chart1.ChartAreas[0].AxisX.ScaleView.Position = 1;
                chart1.ChartAreas[0].AxisX.ScaleView.Size = 10;
                chart1.ChartAreas[0].AxisX.ScrollBar.ButtonColor = System.Drawing.Color.Silver;
                chart1.ChartAreas[0].AxisX.ScrollBar.LineColor = System.Drawing.Color.Black;
                chart1.ChartAreas[0].AxisY.Interval = 5;
                chart1.ChartAreas[0].AxisY.Minimum = 0;
                chart1.ChartAreas[0].AxisY.Maximum = 50;


                for (int i = 0; i <4; i++)
                {
                    chart1.Series[i].IsValueShownAsLabel = true;
                    chart1.Series[i].CustomProperties = "LabelStyle=Top";
                }
            }
        }
        public double ArrayListMax(ArrayList sampleList)
        {
            try
            {
                double maxDevation = 0.0;
                int totalCount = sampleList.Count;
                if (totalCount >= 1)
                {
                    double max = Double.Parse(sampleList[0].ToString());
                    for (int i = 0; i < totalCount; i++)
                    {
                        double temp = Double.Parse(sampleList[i].ToString());
                        if (temp > max)
                        {
                            max = temp;
                        }
                    } maxDevation = max;
                }
                return maxDevation;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public double stdev(ArrayList ValList)
        {
            double avg = Avg(ValList);
            double sumstdev = 0;
            foreach (double d in ValList)
            {
                sumstdev = sumstdev + (d - avg) * (d - avg);
            }
            double stdeval = System.Math.Sqrt(sumstdev);
            return System.Math.Round(stdeval, 5);
        }
        public double Avg(ArrayList al)
        {
            double val = 0.0;
            int count = 0;
            foreach (double el in al)
            {
                val += el;
                count++;
            }
            return val = val / count;
        }
        //获取ArrayList中的最小值
        public double ArrayListMin(ArrayList sampleList)
        {
            try
            {
                double mixDevation = 0.0;
                int totalCount = sampleList.Count;
                if (totalCount >= 1)
                {
                    double min = Double.Parse(sampleList[0].ToString());
                    for (int i = 0; i < totalCount; i++)
                    {
                        double temp = Double.Parse(sampleList[i].ToString());
                        if (min > temp)
                        {
                            min = temp;
                        }
                    } mixDevation = min;
                }
                return mixDevation;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ArrayList Standard(ArrayList val)
        {
            ArrayList returnVal = new ArrayList();
            double max = ArrayListMax(val);
            double min = ArrayListMin(val);
            double avg = Avg(val);
            double dev = stdev(val);
            foreach (double el in val)
            {
                //returnVal.Add(10 * (el - avg) / (max - min));
                if (dev - 0 < 0.0000001)
                    returnVal.Add(0);
                else
                    returnVal.Add((el - avg) / dev);
            }
            return returnVal;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (onlineCheckBox.Checked == false && bianxieCheckBox.Checked == false) {
                MessageBox.Show("请选择要查询的数据源：在线监测数据或便携式采集数据");
                return;
            }


            if (bianDianZhan.Text.Trim() == "" || bianDianZhan.Text == null)
            {
                MessageBox.Show("未选择变电站名称");
                return;
            }
            else if (kaiGuanShi.Text.Trim() == "" || kaiGuanShi.Text == null)
            {
                MessageBox.Show("未选择开关室名称");
                return;
            }
            else if (kaiGuanGui.Text.Trim() == "" || kaiGuanGui.Text == null)
            {
                MessageBox.Show("未选择开关柜编号");
                return;
            }
            else
            {
                int flag = 0;//在线监测
                if (onlineCheckBox.Checked == true && bianxieCheckBox.Checked == true)
                    flag = -1;
                else if (bianxieCheckBox.Checked == true)
                    flag = 1;

                bdz = bianDianZhan.Text.Trim();
                kgs = kaiGuanShi.Text.Trim();
                kgg = kaiGuanGui.Text.Trim();
                dt1 = dateTimePicker1.Value;
                dt2 = dateTimePicker2.Value;
                DataTableForm dtf = new DataTableForm();

                DataBase dataBase = new DataBase();
                dtf.SetDataTable(dataBase.GetDataTableForHistoryForm(dt1, dt2, bdz, kgs, kgg, flag));
                dtf.Show();
            }
        }

        private void HistorySearchForm_Load(object sender, EventArgs e)
        {

            dateTimePicker1.Value = DateTime.Now.Date;
            dateTimePicker2.Value = DateTime.Now.Date;

            DataBase dataBase = new DataBase();
            bianDianZhan.DataSource = dataBase.getAllBianDianZhan();
            bianDianZhan.DisplayMember = "Bdzmc";
            bianDianZhan.Refresh();

            chart1.Series[0].LegendText = "NO2";
            chart1.Series[1].LegendText = "CO";
            chart1.Series[2].LegendText = "NO";
            chart1.Series[3].LegendText = "O3";

            //Color labelColor = Color.Green;
            //int labelWidth = 2;
            //chart1.Series[0].IsValueShownAsLabel = true;
            //chart1.Series[0].LabelForeColor = labelColor;
            //chart1.Series[0].LabelBorderWidth = labelWidth;

            //chart1.Series[1].IsValueShownAsLabel = true;
            //chart1.Series[1].LabelForeColor = labelColor;
            //chart1.Series[1].LabelBorderWidth = labelWidth;

            //chart1.Series[2].IsValueShownAsLabel = true;
            //chart1.Series[2].LabelForeColor = labelColor;
            //chart1.Series[2].LabelBorderWidth = labelWidth;

            //chart1.Series[3].IsValueShownAsLabel = true;
            //chart1.Series[3].LabelForeColor = labelColor;
            //chart1.Series[3].LabelBorderWidth = labelWidth;
            //chart1.Series[4].LegendText = "Tem";
            //chart1.Series[5].LegendText = "RH";
            //chart1.Series[6].LegendText = "Ato";


        }
        private void bianDianZhan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)bianDianZhan.SelectedItem;
            string bdz = drv.Row["Bdzmc"].ToString();
            if ("".Equals(bdz) || null == bdz)
            {
                return;
            }
            DataBase dataBase = new DataBase();
            kaiGuanShi.DataSource = dataBase.getAllKaiGuanShiByBDZ(bdz);
            kaiGuanShi.DisplayMember = "Kgsmc";
            kaiGuanShi.Refresh();
        }

        private void kaiGuanShi_SelectionChangeCommitted(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)bianDianZhan.SelectedItem;
            string bdz = drv.Row["Bdzmc"].ToString();
            DataRowView drv2 = (DataRowView)kaiGuanShi.SelectedItem;
            string kgs = drv2.Row["Kgsmc"].ToString();
            if ("".Equals(kgs) || null == kgs)
            {
                return;
            }
            if ("".Equals(bdz) || null == bdz)
            {
                return;
            }
            DataBase dataBase = new DataBase();
            kaiGuanGui.DataSource = dataBase.getAllKaiGuanGuiByBdzAndKgs(bdz, kgs);
            kaiGuanGui.DisplayMember = "Kggbh";
            kaiGuanGui.Refresh();
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {

        }

        private void skinButton1_Click_1(object sender, EventArgs e)
        {
            
            DataTableForm dtf = new DataTableForm();

            DataBase dataBase = new DataBase();
            dtf.SetDataTable(dataBase.GetAllHistoryForm());
            dtf.Show();
        }

        private void 保存图表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "jpeg文件(*.jpg)|*.jpg";
            sfd.FilterIndex = 1;
            sfd.RestoreDirectory = true;
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                String strSaveFileName = sfd.FileName;
                chart1.SaveImage(strSaveFileName, ChartImageFormat.Jpeg);
                MessageBox.Show("图片保存成功");
            }
        }

        private void kaiGuanShi_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)bianDianZhan.SelectedItem;
            string bdz = drv.Row["Bdzmc"].ToString();
            DataRowView drv2 = (DataRowView)kaiGuanShi.SelectedItem;
            string kgs = drv2.Row["Kgsmc"].ToString();
            if ("".Equals(kgs) || null == kgs)
            {
                return;
            }
            if ("".Equals(bdz) || null == bdz)
            {
                return;
            }
            DataBase dataBase = new DataBase();
            kaiGuanGui.DataSource = dataBase.getAllKaiGuanGuiByBdzAndKgs(bdz, kgs);
            kaiGuanGui.DisplayMember = "Kggbh";
            kaiGuanGui.Refresh();
        }

        private void bianDianZhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)bianDianZhan.SelectedItem;
            string bdz = drv.Row["Bdzmc"].ToString();
            if ("".Equals(bdz) || null == bdz)
            {
                return;
            }
            DataBase dataBase = new DataBase();
            kaiGuanShi.DataSource = dataBase.getAllKaiGuanShiByBDZ(bdz);
            kaiGuanShi.DisplayMember = "Kgsmc";
            kaiGuanShi.Refresh();
        }
    }
}
